package sky.wicket.panels;

import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.ajax.markup.html.AjaxFallbackLink;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.list.ListItem;
import org.apache.wicket.markup.html.list.ListView;
import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.model.IModel;
import org.apache.wicket.model.Model;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import sky.treasure.Area;
import sky.treasure.AreaView;
import sky.treasure.Cell;
import sky.treasure.Coordinates;
import sky.treasure.properties.ObstacleProperty;
import sky.treasure.properties.TrapProperty;
import sky.treasure.properties.TreasureProperty;

import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: Gauss
 * Date: 05.10.13
 * Time: 13:03
 * To change this template use File | Settings | File Templates.
 */
public class AreaPanel extends Panel {

    private boolean isExpandable = false;
    private Area area;
    private Label[][] labelArray;
    private Model[][] modelArray;

    static final Logger log = LoggerFactory.getLogger(AirportPanel.class);

    public AreaPanel(String id, IModel<?> model) {
        super(id, model);
        try {
            area = (Area) model.getObject();
        } catch (ClassCastException e) {
            log.error("Cast exception. Expected Area.class but found " + model.getObject().getClass().toString() + ".class");
            throw new RuntimeException(e);
        }
        addElementsToPanel((AreaView) area);
    }

    private void addElementsToPanel(final AreaView area) {
        final List list = area.getList();
        final AreaListView listView = new AreaListView("area", list);
        labelArray = new Label[area.getMeasuresTo().getX()][area.getMeasuresTo().getY()];
        modelArray = new Model[area.getMeasuresTo().getX()][area.getMeasuresTo().getY()];
        add(listView);
        add(new AjaxFallbackLink("nextStep") {
            @Override
            public void onClick(AjaxRequestTarget ajaxRequestTarget) {

            }
        });
    }

    private class AreaListView extends ListView {

        public AreaListView(String id) {
            super(id);
        }

        private AreaListView(String id, List list) {
            super(id, list);
        }

        @Override
        protected void populateItem(ListItem listItem) {
            listItem.add(new AreaRowListView("row", (List) listItem.getModelObject()));
        }
    }

    private class AreaRowListView extends ListView {
        public AreaRowListView(String id) {
            super(id);
        }

        private AreaRowListView(String id, List list) {
            super(id, list);
        }

        @Override
        protected void populateItem(ListItem listItem) {
            Cell cell = (Cell) listItem.getModelObject();
            final Label label;
            final Model<String> model = new Model<String>("_");
            label = new Label("cell", updateModel(model, cell.getCoordinates()));
            labelArray[cell.getCoordinates().getX()][cell.getCoordinates().getY()] = label;
            modelArray[cell.getCoordinates().getX()][cell.getCoordinates().getY()] = model;
            label.setOutputMarkupId(true);
            listItem.add(label);
        }
    }

    private Model updateModel(Model model, Coordinates coordinates) {
        Cell cell = area.getCell(coordinates);
        if (cell.getProperties()!=null) {
            if (cell.getProperties().get(0).getClass() == ObstacleProperty.class) {
                model.setObject("#");
            }
            if (cell.getProperties().get(0).getClass() == TrapProperty.class) {
                model.setObject("w");
            }
            if (cell.getProperties().get(0).getClass() == TreasureProperty.class) {
                model.setObject("!");
            }
        }
        return model;
    }
}
